package com.specialtreasure.freightdepot.dialog;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.app.core.dialog.BaseDialogFragment;
import com.specialtreasure.freightdepot.R;

/**
 * Created by Administrator on 2017/8/15 0015.
 * 加载进度框
 */

public class LoadingDialogFragment extends BaseDialogFragment {
    String text;
    private TextView tv_text;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.view_tips_loading, null);
        outsideDissmiss();
        initView(view);
        return view;
    }

    public static LoadingDialogFragment newInstance(String text) {
        LoadingDialogFragment dialogFragment = new LoadingDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("text", text);
        dialogFragment.setArguments(bundle);
        return dialogFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NORMAL,R.style.commonDialog);
        text = getArguments().getString("text");
    }


    /**
     * 点击外部不消失
     * 点击返回键不消失
     */
    private void outsideDissmiss() {
        setStyle(STYLE_NORMAL, R.style.commonDialog);
        // 点击外部不消失
        getDialog().setCanceledOnTouchOutside(false);
        //点击返回键不消失，需要监听OnKeyListener:
        getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });

    }

    private void initView(View view) {
        tv_text = (TextView) view.findViewById(R.id.tv_text);


        if (!TextUtils.isEmpty(text)){
            tv_text.setText(text);
        }

    }
}
